﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Huayuan.Manage.Common;
using Hyt.Core.Interceptors;
using Hyt.Passport;
using Hyt.Web.Mvc.Partial;
using Hyt.Web.Mvc.Partial.Models;
using Huayuan.Manage.Areas.Passport.Models;
using Huayuan.Manage.Models.Util;
using Hyt.Web.Mvc.Controls;
using Hyt.Core;
using Hyt.Web.Mvc;
using Hyt.Passport.Models;

namespace Huayuan.Manage.Areas.Passport.Controllers
{
    public class DepartmentManageController : BaseController
    {
        #region 列表
        public ActionResult Index()
        {
            return View("Index");
        }

        public ActionResult List()
        {
            using (var ufw = new WebUnitOfWork())
            {
                var department = ufw.ServiceCenter.GetAllDepartment().OrderBy(t=>t.Code);
                var treedata = new TreeTableData<Object>
                {
                    //从treenode中找到主键
                    IdFinder = t => ((Department)t).Code,
                    //根据treenode和数据源找到父node的主键
                    ParentIdFinder = (list, t) =>
                    {
                        var thisdepart = (Department)t;
                        var parent = list.FirstOrDefault(item =>
                        {
                            var depart = (Department)item;
                            if (depart.Code.Length == thisdepart.Code.Length - 2 && thisdepart.Code.StartsWith(depart.Code))
                            {
                                return true;
                            }
                            return false;
                        });
                        if (parent == null) return "";
                        return ((Department)parent).Code;
                    },
                    //把数据源格式化成treenode
                    Items = new List<TreeTableNode<Object>>
                    {
                        new TreeTableNode<Object>
                        {
                            Name = "名称",
                            GetContent = t => ((Department) t).Name
                        },
                        new TreeTableNode<Object>
                        {
                            Name = "操作",
                            GetContent = t => {
                                if (((Department)t).Name == "技术支持部") return "";
                                return
                                Buttons.CalljsButton("删除", "delete", "{method:hyt_link_afterConfirm,confirm:'确定删除此部门？',url:'" + Url.Action("Delete", new { Id = ((Department)t).Id }) + "'}") +
                                Buttons.DialogButton(Url.Action("Modify", new { Id = ((Department)t).Id }), "修改", "edit", "{intent:'modify',width:'50%',height:'50%'}")+
                                Buttons.DialogButton(Url.Action("Create", new { ParentCode = ((Department)t).Code }), "添加子部门", "add", "{intent:'add',width:'50%',height:'50%'}")
                                ;
                            }
                        }
                    },
                    //数据源
                    Data = new List<Object>(department.ToList())
                };

                ufw.Commit();

                return View(ViewPath.TreeTable, treedata);
            }

        }
        #endregion

        #region 添加
        public ActionResult Create(DepartmentCreateModel.View model)
        {
            if (!string.IsNullOrWhiteSpace(model.ParentCode))
            {
                using (var ufw = new WebUnitOfWork())
                {
                    var department = ufw.ServiceCenter.GetDepartmentByCode(model.ParentCode);
                    if (department != null)
                    {
                        model.ParentName = department.Name;
                    }
                    ufw.Commit();
                }
            }
            return View(new DepartmentCreateModel { VW = model });
        }

        [HttpPost]
        public JsonResult Create(DepartmentCreateModel model)
        {
            using (var ufw = new WebUnitOfWork())
            {
                ufw.ServiceCenter.CreateDepartment(model.PO.Name,model.PO.ParentCode);
                ufw.Commit();
                return Const.JsonSuccess;
            }

        }
        #endregion

        #region 删除
        public ActionResult Delete(Guid id)
        {
            using (var ufw = new WebUnitOfWork())
            {
                ufw.ServiceCenter.DeleteDepartment(id);
                ufw.Commit();
            }
            //Prompt("删除成功",2000);
            return RedirectToAction("Index");
        }
        #endregion

        #region 修改
        public ActionResult Modify(Guid id)
        {
            using (var ufw = new WebUnitOfWork())
            {
                var dprt = ufw.ServiceCenter.GetDepartmentById(id);
                Guard.Check(dprt).IfIsNull().ThrowNoExisted("部门");
                ufw.Commit();
                return View(dprt);
            }
        }

        [HttpPost]
        public JsonResult Modify(Department model)
        {
            using (var ufw = new WebUnitOfWork())
            {
                ufw.ServiceCenter.ModifyDepartmentName(model);
                ufw.Commit();
                NotifyOpenner(Common.JS.Intents.action_dataischange);
                return Const.JsonSuccess;
            }
        }

        #endregion
        
        #region 选择
        public ActionResult Select()
        {
            return View();
        }

        public ActionResult SelectList()
        {
            using (var ufw = new WebUnitOfWork())
            {
                var department = ufw.ServiceCenter.GetAllDepartment().OrderBy(t => t.Code);
                var treedata = new TreeTableData<Object>
                {
                    //从treenode中找到主键
                    IdFinder = t => ((Department)t).Code,
                    //根据treenode和数据源找到父node的主键
                    ParentIdFinder = (list, t) =>
                    {
                        var thisdepart = (Department)t;
                        var parent = list.FirstOrDefault(item =>
                        {
                            var depart = (Department)item;
                            if (depart.Code.Length == thisdepart.Code.Length - 2 && thisdepart.Code.StartsWith(depart.Code))
                            {
                                return true;
                            }
                            return false;
                        });
                        if (parent == null) return "";
                        return ((Department)parent).Code;
                    },
                    //把数据源格式化成treenode
                    Items = new List<TreeTableNode<Object>>
                    {
                        new TreeTableNode<Object>
                        {
                            Name = "名称",
                            GetContent = t => ((Department) t).Name
                        },
                        new TreeTableNode<Object>
                        {
                            Name = "操作",
                            GetContent = t => {
                                return
                                Buttons.CalljsButton("选取", "choose", "{method:hyt_link_selectback,backdata:{title:'" + ((Department)t).Name + "',id:'" + ((Department)t).Id + "'}}")
                                ;
                            }
                        }
                    },
                    //数据源
                    Data = new List<Object>(department.ToList())
                };

                ufw.Commit();

                return View(ViewPath.TreeTable, treedata);
            }

        }
        #endregion

    }
}
